package com.service.component.permissions.controller;

import com.service.component.common.CommonResult;
import com.service.component.common.enumerate.OperateTypeEnum;
import com.service.component.log.aspect.SysOperateLog;
import com.service.component.permissions.entity.Permission;
import com.service.component.permissions.services.PermissionService;
import com.service.component.relation.entity.GroupPremRelation;
import com.service.component.relation.entity.RolePremRelation;
import com.service.component.relation.entity.UserPremRelation;
import com.service.component.relation.services.RelationServices;
import com.service.component.relation.services.impl.RelationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：Calvin
 * @create ：2021/7/22 13:14
 * @description：权限操作管理
 * @modified By：
 * @version: 1.0.0$
 */
@Slf4j
@RestController
@Api(value = "权限管理")
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RelationServices relationServices;

    @PostMapping("insertPerm")
    @ApiOperation(value = "新增权限", notes = "新增权限")
    @SysOperateLog(descrption = "新增权限",operateType = OperateTypeEnum.ADD)
    public CommonResult insertPermission(@RequestBody Permission permission){
        Permission permissionByName = permissionService.getPermissionByPermName(permission.getPermNameEN());
        if(ObjectUtils.isEmpty(permissionByName)){
            return permissionService.insertPermission(permission);
        }
        return CommonResult.commonResult(CommonResult.ResultCode.DATA_EXIST);
    }

    @GetMapping("getPermissionByUserId/{userId}")
    @ApiOperation(value = "根据用户id查询该用户拥有的所有权限", notes = "根据用户id查询该用户拥有的所有权限")
    @SysOperateLog(descrption = "根据用户id查询该用户拥有的所有权限",operateType = OperateTypeEnum.ADD)
    public CommonResult getPermissionByUserId(@PathVariable String userId){
        return CommonResult.success(permissionService.getPermissionByUserId(userId));
    }

    @PostMapping("authorizationGroupPrem")
    @ApiOperation(value = "给组授权", notes = "给组授权")
    @SysOperateLog(descrption = "给组授权",operateType = OperateTypeEnum.ADD)
    public CommonResult authorizationGroupPrem(@RequestBody GroupPremRelation groupPremRelation){
        return relationServices.insertOrUpdateRelation(groupPremRelation);
    }

    @PostMapping("authorizationUserPrem")
    @ApiOperation(value = "给用户授权", notes = "给用户授权")
    @SysOperateLog(descrption = "给用户授权",operateType = OperateTypeEnum.ADD)
    public CommonResult authorizationUserPrem(@RequestBody UserPremRelation userPremRelation){
        return relationServices.insertOrUpdateRelation(userPremRelation);
    }

    @PostMapping("authorizationRolePrem")
    @ApiOperation(value = "给角色授权", notes = "给角色授权")
    @SysOperateLog(descrption = "给角色授权",operateType = OperateTypeEnum.ADD)
    public CommonResult authorizationRolePrem(@RequestBody RolePremRelation rolePremRelation){
        return relationServices.insertOrUpdateRelation(rolePremRelation);
    }
}
